﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using ProjectFootball.MVC.Models.EntityFramework.Entities;

namespace ProjectFootball.MVC.Models.EntityFramework.Contexts
{
    public class PFDBContext : DbContext
    {
        public PFDBContext()
            : base("name=PFDBContext")
        {
        }

        public DbSet<Campaign> Campaigns { get; set; }
        public DbSet<Competition> Competitions { get; set; }
        public DbSet<Country> Countries { get; set; }
        public DbSet<LookupCompetition> LookupCompetitions { get; set; }
        public DbSet<LookupMatch> LookupMatches { get; set; }
        public DbSet<LookupPerson> LookupPeople { get; set; }
        public DbSet<LookupTeam> LookupTeams { get; set; }
        public DbSet<Match> Matches { get; set; }
        public DbSet<MatchEvent> MatchEvents { get; set; }
        public DbSet<Person> People { get; set; }
        public DbSet<Team> Teams { get; set; }
        public DbSet<TeamVersion> TeamVersions { get; set; }
        public DbSet<Venue> Venues { get; set; }
        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Membership> Memberships { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<UsersInRole> UsersInRoles { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Campaign>().HasRequired(m => m.AuditUser).WithMany().HasForeignKey(m => m.AuditUserId).WillCascadeOnDelete(false);
            modelBuilder.Entity<Campaign>().HasRequired(m => m.OwnerUser).WithMany().HasForeignKey(m => m.OwnerUserId).WillCascadeOnDelete(false);

            modelBuilder.Entity<Competition>().HasRequired(m => m.AuditUser).WithMany().HasForeignKey(m => m.AuditUserId).WillCascadeOnDelete(false);
            modelBuilder.Entity<Competition>().HasRequired(m => m.OwnerUser).WithMany().HasForeignKey(m => m.OwnerUserId).WillCascadeOnDelete(false);

            modelBuilder.Entity<Country>().HasRequired(m => m.AuditUser).WithMany().HasForeignKey(m => m.AuditUserId).WillCascadeOnDelete(false);
            modelBuilder.Entity<Country>().HasRequired(m => m.OwnerUser).WithMany().HasForeignKey(m => m.OwnerUserId).WillCascadeOnDelete(false);

            modelBuilder.Entity<Match>().HasRequired(a => a.Team1).WithMany().HasForeignKey(u => u.Team1Guid).WillCascadeOnDelete(false);
            modelBuilder.Entity<Match>().HasRequired(a => a.Team2).WithMany().HasForeignKey(u => u.Team2Guid).WillCascadeOnDelete(false);
            modelBuilder.Entity<Match>().HasRequired(m => m.AuditUser).WithMany().HasForeignKey(m => m.AuditUserId).WillCascadeOnDelete(false);
            modelBuilder.Entity<Match>().HasRequired(m => m.OwnerUser).WithMany().HasForeignKey(m => m.OwnerUserId).WillCascadeOnDelete(false);

            modelBuilder.Entity<MatchEvent>().HasRequired(m => m.AuditUser).WithMany().HasForeignKey(m => m.AuditUserId).WillCascadeOnDelete(false);
            modelBuilder.Entity<MatchEvent>().HasRequired(m => m.OwnerUser).WithMany().HasForeignKey(m => m.OwnerUserId).WillCascadeOnDelete(false);

            modelBuilder.Entity<Person>().HasRequired(m => m.AuditUser).WithMany().HasForeignKey(m => m.AuditUserId).WillCascadeOnDelete(false);
            modelBuilder.Entity<Person>().HasRequired(m => m.OwnerUser).WithMany().HasForeignKey(m => m.OwnerUserId).WillCascadeOnDelete(false);

            modelBuilder.Entity<TeamVersion>().HasRequired(m => m.AuditUser).WithMany().HasForeignKey(m => m.AuditUserId).WillCascadeOnDelete(false);
            modelBuilder.Entity<TeamVersion>().HasRequired(m => m.OwnerUser).WithMany().HasForeignKey(m => m.OwnerUserId).WillCascadeOnDelete(false);

            modelBuilder.Entity<Venue>().HasRequired(m => m.AuditUser).WithMany().HasForeignKey(m => m.AuditUserId).WillCascadeOnDelete(false);
            modelBuilder.Entity<Venue>().HasRequired(m => m.OwnerUser).WithMany().HasForeignKey(m => m.OwnerUserId).WillCascadeOnDelete(false);
        }
    }
}